/*
 * wcserv.c
 *
 *  Created on: Mar 27, 2012
 *      Author: root
 */

#include	"unp.h"
void sig_chld(int signo);

int
main(int argc, char **argv)
{
	int					listenfd, connfd;
	int line_count=0;
	int full_bytecount=0;
	int full_wordcount=0;
	int full_linecount=0;
	int byte_count=0;
	pid_t				childpid;
	socklen_t			clilen;
	struct sockaddr_in	cliaddr, servaddr;
	char		buf[MAXLINE];
	char		buf1[MAXLINE];
	char		buf2[MAXLINE];


	//void				sig_chld(int);
	ssize_t		n;

	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(SERV_PORT);

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	Signal(SIGCHLD, sig_chld);	/* must call waitpid() */

	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		if ( (connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0)
		{

			if (errno == EINTR)
			{

				continue;		/* back to for() */
			}
			else
				err_sys("accept error");
		}
		if ( (childpid = Fork()) == 0) {	/* child process */
			Close(listenfd);	/* close listening socket */

			//str_echo(connfd);	/* process the request */




			again:
				while ( (n = read(connfd, buf, MAXLINE)) > 0)
				{
					if(!strcmp(buf,".\n"))
					{
						printf("-------------close-------------\n");

						break;
					}

					line_count++;

					printf("message from client : %s",buf);
					byte_count=strlen(buf)-1;
					sprintf(buf2,"%s",buf);
					char *token=NULL;
					char s1[]="\n";

					token=strtok(buf2,s1);
					while(token !=NULL)
					{
					token = strtok(NULL,s1);
					}

					Fputs(buf2,stdout);
					//printf("message byte count # : %d\n",strlen(buf)-1);

					//strcat(buf,buf1);
					//Fputs(buf,stdout);
					//Writen(connfd, buf, strlen(buf));


					char s2[]=" \n";

					int wordcount=0;
					token=strtok(buf,s2);
					while(token !=NULL)
					{

						token = strtok(NULL,s2);
						wordcount++;
					}
					printf("line count # is : %d\n", line_count);
					printf("word count is : %d\n",wordcount);
					printf("byte count # : %d\n",byte_count);
					printf("word byte count # : %d\n\n",byte_count-wordcount+1);
					sprintf(buf1,"    %d,   %d,    %d\n",line_count,wordcount,byte_count);

					printf("-----------------------\n");
					memset(buf,0,strlen(buf));
					sprintf(buf,"%s",buf2);
					fflush(stdout);
					Fputs(buf,stdout);

					printf("-----------------------\n");

					strcat(buf,buf1);
					Fputs(buf,stdout);
					Writen(connfd, buf, strlen(buf));
					memset(buf,0,strlen(buf));
					Fputs(buf,stdout);



				}
				if (n < 0 && errno == EINTR)
					goto again;
				else if (n < 0)
					err_sys("str_echo: read error");

				//byte count , word count, line count
			exit(0);
		}

		Close(connfd);			/* parent closes connected socket */
	}
}

void sig_chld(int signo)
{
	pid_t	pid;
	int stat;

	while((pid=waitpid(-1,&stat,WNOHANG))>0)
		printf("child %d terminated\n",pid);

	return;
}





